﻿using App.Dal;
using App.Desk.Extend;
using App.Desk.UserControls;
using App.Model;
using App.Untiys;
using S7.Net;
using SqlSugar;
using Sunny.UI;
using Sunny.UI.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace App.Desk.Pro.Monitors
{
    public partial class FrmRunState : UIPage
    {
        public List<PLCParentParaModel> plcParas = new List<PLCParentParaModel>();
        public FrmRunState()
        {
            InitializeComponent();
            SetInfo();
            SamplingPLCHelper.Instance.PlcDataReadOut += OnPlcDataReadOut;
        }

        public void SetInfo()
        {
            plcParas.Add(new PLCParentParaModel
            {
                GroupName = "采样方式",
                PLCStr = new List<string> {
                    "V38.0|1|手动",
                    "V38.1|1|半自动" ,
                    "V38.2|1|自动"
                }
            });

            plcParas.Add(new PLCParentParaModel
            {
                GroupName = "采样流程",
                PLCStr = new List<string> {
                    "V20.0|1|暂停",
                    "V20.1|1|正在复位流程",
                    "V20.2|1|手动",
                    "V20.3|1|自动准备好",
                    "V20.4|1|车厢检测流程",
                    "V20.5|1|去目标点流程",
                    "V20.6|1|下降流程",
                    "V21.0|1|下降等待到位",
                    "V21.1|1|上升流程",
                    "V21.2|1|回卸料点",
                    "V21.3|1|给料皮带摆至接料位",
                    "V21.4|1|集料斗开",
                    //"V21.5|1|正在制样",
                    "V21.6|1|给料皮带摆至采样位",
                    "V21.7|1|集料斗关",
                    "V22.2|1|正在校桶",
                    "V22.3|1|正在转桶",
                    "V39.4|1|人工取桶选择：正在取桶，禁止操作设备"
                }
            });
            plcParas.Add(new PLCParentParaModel
            {
                GroupName = "超时报警",
                PLCStr = new List<string> {
                    "V64.1|1|集斗门开关动作超时(V64.1)",
                    "V64.2|1|给料皮带摆动动作超时(V64.2)",
                    "V64.4|1|弃料门开关动作超时,采样可能堵塞，请检查弃料门(V64.4)",
                    "V39.3|1|车厢检测失败，请将大车退回并重新开始采样(V39.3)"
                }
            });
            plcParas.Add(new PLCParentParaModel
            {
                GroupName = "锁状态",
                PLCStr = new List<string>
                {
                    "V41.0|1|解锁状态，禁止采样",
                    "V41.0|0|联锁状态"
                }
            });
            plcParas.Add(new PLCParentParaModel
            {
                GroupName = "发送给翻车机信号",
                PLCStr = new List<string>
                {
                    "V41.1|1|允许牵车",
                    "V41.1|0|禁止牵车"
                }
            });
            plcParas.Add(new PLCParentParaModel
            {
                GroupName = "接收到翻车机信号",
                PLCStr = new List<string>
                {
                    "V41.5|1|允许采样",
                    "V41.5|0|禁止采样"
                }
            });
            plcParas.Add(new PLCParentParaModel
            {
                GroupName = "集料斗打开方式",
                PLCStr = new List<string>
                {
                    "V38.6|1|多次打开",
                    "V38.6|0|一次打开"
                }
            });
            plcParas.Add(new PLCParentParaModel
            {
                GroupName = "故障报警",
                PLCStr = new List<string>
                {
                    "V56.0|1|大车故障(V56.0)",
                    "V56.1|1|小车故障(V56.1)",
                    "V56.2|1|升降故障(V56.2)",
                    "V56.3|1|螺旋故障(V56.3)",
                    "V56.4|1|集料斗故障(V56.4)",
                    "V56.5|1|给料皮带故障(V56.5)",
                    "V56.6|1|给料皮带摆动故障(V56.6)",
                    "V57.0|1|刮板故障(V57.0)",
                    "V57.1|1|破碎故障(V57.1)",
                    "V57.2|1|缩分故障(V57.2)",
                    "V57.3|1|集样器故障(V57.3)",
                    "V57.4|1|弃料门故障(V57.4)",
                    "V57.7|1|缩分皮带故障(V57.7)",
                    "V59.0|1|相序出错(V59.0)",
                    "V26.0|1|控制电源(V26.0)",
                    "V26.1|1|24V回测(V26.1)"
                }
            });
            plcParas.Add(new PLCParentParaModel
            {
                GroupName = "限位警告",
                PLCStr = new List<string>
                {
                    "V50.0|1|大车已到前限位,不能前进(V50.0)",
                    "V50.1|1|大车已到后限位,不能后退(V50.1)",
                    "V50.2|1|小车已到左限位,不能左行(V50.2)",
                    "V50.3|1|小车已到右限位,不能右行(V50.3)",
                    "V50.4|1|采样头已到上限位,不能上升(V50.4)",
                    "V50.5|1|采样头已到下限位,不能下降(V50.5)",
                    "V50.6|1|采样头没到上限位,大小车不能动(V50.6)",
                    "V51.2|1|集料斗没关到位，不能升降(V51.2)",
                    "V51.3|1|给料机没摆开到位，不能升降(V51.3)",
                    "V51.5|1|集料斗没关到位，给料机不能摆动(V51.5)",
                    "V51.6|1|采样头没到上限位，给料机不能摆动(V51.6)",
                    "V51.7|1|给料机不在摆开位或接料位，集料斗不能开关(V51.7)",
                    "V52.0|1|不能补采，螺旋下降过深(V52.0)",
                    "V52.1|1|不在允许下降区，不能下降(V52.1)",
                    "V52.2|1|车厢检测超时，请重新开始采样(V52.2)",
                    "V52.3|1|车厢检测碰到极限位，请重新开始采样(V52.3)",
                    "V52.4|1|大车去目标点碰到极限位，请重新开始采样(V52.4)",
                    "V52.5|1|大车目标点坐标出错(V52.5)",
                    "V52.6|1|小车目标点坐标出错(V52.6)",
                    "V53.0|1|升降目标点坐标出错(V53.0)",
                    "V53.1|1|目标点数出错(V53.1)",
                    "V53.2|1|目标桶号出错(V53.2)",
                    "V53.3|1|人工取桶，禁止操作(V53.3)",
                    "V59.1|1|大车坐标超限(V59.1)",
                    "V59.2|1|小车坐标超限(V59.2)",
                    "V59.3|1|升降坐标超限(V59.3)",
                    "V59.4|1|采样中复位，联锁或允许采样信号丢失(V59.4)"
                }
            });
            plcParas.Add(new PLCParentParaModel
            {
                GroupName = "接通报警",
                PLCStr = new List<string>
                {
                    "V60.0|1|大车前后限位同时接通(V60.0)",
                    "V60.1|1|小车左右限位同时接通(V60.1)",
                    "V60.2|1|升降上下限位同时接通(V60.2)",
                    "V60.3|1|集料斗开关限位同时接通(V60.3)",
                    "V60.4|1|接料摆开限位和接料限位同时接通(V60.4)",
                    "V60.5|1|弃料斗开关限位同时接通(V60.5)",
                    "V60.6|1|集样器计数故障(V60.6)",
                    "V62.1|1|大车前进6S后限位仍然接通(V62.1)",
                    "V62.2|1|大车后退6S前限位仍然接通(V62.2)",
                    "V62.3|1|大车计数故障(V62.3)",
                    "V62.4|1|小车前进6S后限位仍然接通(V62.4)",
                    "V62.5|1|小车后退6S前限位仍然接通(V62.5)",
                    "V62.6|1|小车计数故障(V62.6)",
                    "V63.0|1|升降上升6S下限位仍然接通(V63.0)",
                    "V63.1|1|升降下降6S上限位仍然接通(V63.1)",
                    "V63.2|1|升降计数故障(V63.2)"
                }
            });
        }

        private void FrmRunState_Load(object sender, EventArgs e)
        {
            UcInfoButton ucInfoButton;
            foreach (var item in plcParas)
            {
                ucInfoButton = new UcInfoButton();
                ucInfoButton.Name = item.GroupName;
                ucInfoButton.SetText(item.GroupName + ":");
                ucInfoButton.SetValue("");
                //ucInfoButton.Width = 180;
                ucInfoButton.PlusParmeter = item;
                flowBox.Add(ucInfoButton);
            }

            //var controls = flowBox.GetAllControls();
            //foreach (var control in controls)
            //{
            //    if(control is UcInfoButton)
            //    {
            //        var tempControl = (UcInfoButton)control;
            //        tempControl.PlusParmeter.PLCStr.ForEach(plc =>
            //        {
            //            var addr = plc.ToSplitArrayEx('|');
            //            UIHelper.SetInfoControl((UcInfoButton)control, addr[2]);
            //        });
            //    }
            //}
        }

        public int PLV(short[] _arrReads, string address)
        {
            int[] _aryBits;
            var ints = address.To_PLC_V_SPLIT();
            if (ints.IsNotNull())
            {
                _aryBits = DataConvert.Ushort2Binary((int)(_arrReads[ints[0] / 2]));
                return _aryBits[ints[1]];
            }
            return -1;
        }

        private void OnPlcDataReadOut(object send, ActiveReadEventArgs e)
        {
            short[] _arrReads = (short[])e.E_ReadValue;
            var controls = flowBox.GetAllControls();
            foreach (var control in controls)
            {
                if (control is UcInfoButton)
                {
                    var tempControl = (UcInfoButton)control;
                    tempControl.PlusParmeter.PLCStr.ForEach(plc =>
                    {
                        var addr = plc.ToSplitArrayEx('|');
                        if (PLV(_arrReads, addr[0]) == addr[1].ToIntEx())
                        {
                            if (((UcInfoButton)control).GetValue() != addr[2])
                            {
                                UIHelper.SetInfoControl((UcInfoButton)control, addr[2]);
                                if (new string[] { "接通报警", "故障报警", "限位警告" }.Contains(tempControl.PlusParmeter.GroupName))
                                {
                                    LogHelper.DoWriteLogType(LogType.RunError, $"{addr[0]} / {addr[2]}");
                                    ConfigServices.AddErrors(new ProErrors
                                    {
                                        Errors = addr[2],
                                        GroupName = tempControl.PlusParmeter.GroupName,
                                        PLCAddr = addr[0]
                                    });
                                }
                            }
                        }
                    });
                }
            }
        }
    }
}
